package world.criver.h50529.function;

import javafx.application.Platform;
import world.criver.crkey.crKey;
import world.criver.h50529.data.daControllers;
import world.criver.h50529.data.daData;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class funTray {
    public void add(){
        try {
            if (!SystemTray.isSupported()) return;
            SystemTray tray = SystemTray.getSystemTray();
            Image icon = Toolkit.getDefaultToolkit().getImage(funTray.class.getResource(daData.resources_material_path + "logo_128x128.png"));
            TrayIcon trayIcon = new TrayIcon(icon, "拓思通");
            trayIcon.setImageAutoSize(true);
            trayIcon.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    Platform.runLater(() -> {
                        if (e.getButton() == 1) {
                            long t = System.currentTimeMillis(),y=daData.tray_timestamp;
                            daData.tray_timestamp = t;
                            if (t - y > 200) return;
                            daData.master.show();
//                            new crKey("h50529");
                        }
                        if (e.getButton() != 3) return;
                        daData.popMenu.show();
                        daData.popMenu.setX(e.getX() - daData.popMenu.getWidth());
                        daData.popMenu.setY(e.getY() - daData.popMenu.getHeight());
                        daControllers.popMenu.updateMenus();
                    });
                }

                @Override
                public void mousePressed(MouseEvent e) {

                }

                @Override
                public void mouseReleased(MouseEvent e) {

                }

                @Override
                public void mouseEntered(MouseEvent e) {

                }

                @Override
                public void mouseExited(MouseEvent e) {

                }
            });
            tray.add(trayIcon);
            daData.tray=trayIcon;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    public void remove(){
        if(daData.tray==null)return;
        SystemTray.getSystemTray().remove(daData.tray);
    }
}
